package com.vasthu.gui;

import com.vasthu.api.domain.Bill;
import com.vasthu.api.domain.BillInfo;
import com.vasthu.api.domain.Stock;
import com.vasthu.api.service.StockManagerService;
import com.vasthu.core.service.StockManagerServiceImpl;
import com.vasthu.api.service.BillManagerService;
import com.vasthu.core.service.BillManagerServiceImpl;
import com.vasthu.gui.utility.SpringUtilities;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;

public class VstBillDataForm extends JPanel {

    private JLabel caption;
    private JTable dataTable;
    private JButton searchItem;
    private Component spacer;
    private JButton addBill;
    private JButton updateBill;
    private JButton cancelBill;
    private BillInfo billInfo;
    private BillManagerService billManagerService = null;

    public VstBillDataForm() {
        billManagerService = new BillManagerServiceImpl();
        billInfo = new BillInfo(new Bill());
        initUi();
        initActions();
    }

    private void initUi() {
        setLayout(new BorderLayout());

        caption = new JLabel("New Bill", JLabel.CENTER);
        caption.setFont(new Font("Monospaced 12", Font.BOLD, 14));
        dataTable = new JTable();
        dataTable.setModel(new VstBillTableModel());
        dataTable.setFont(new Font("Monospaced 12", Font.PLAIN, 16));
        dataTable.getTableHeader().setFont(new Font("Arial", Font.BOLD, 12));
        dataTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

        /**
         * setting column widths
         */
        //int totalWidth = dataTable.getColumnModel().getTotalColumnWidth();
        dataTable.getColumnModel().getColumn(0).setPreferredWidth(50);
        dataTable.getColumnModel().getColumn(1).setPreferredWidth(250);
        dataTable.getColumnModel().getColumn(2).setPreferredWidth(50);
        dataTable.getColumnModel().getColumn(3).setPreferredWidth(50);
        dataTable.getColumnModel().getColumn(4).setPreferredWidth(50);
        dataTable.getColumnModel().getColumn(5).setPreferredWidth(50);

        JPanel statData = new JPanel();

        searchItem = new JButton("Add Item [F5]");
        searchItem.setPreferredSize(new Dimension(150, 40));
        searchItem.setMaximumSize(new Dimension(150, 40));
        addBill = new JButton("ADD");
        addBill.setPreferredSize(new Dimension(100, 40));
        addBill.setMaximumSize(new Dimension(100, 40));
        updateBill = new JButton("UPDATE");
        updateBill.setPreferredSize(new Dimension(100, 40));
        updateBill.setMaximumSize(new Dimension(100, 40));
        cancelBill = new JButton("CANCEL");
        cancelBill.setPreferredSize(new Dimension(100, 40));
        cancelBill.setMaximumSize(new Dimension(100, 40));

        JPanel base = new JPanel(new SpringLayout());

        JPanel topPanel = new JPanel(new FlowLayout());
        topPanel.add(caption);
        topPanel.setPreferredSize(new Dimension(Integer.MAX_VALUE, 25));
        topPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
        base.add(topPanel);

        JScrollPane pane = new JScrollPane(dataTable);
        pane.setPreferredSize(new Dimension(Integer.MAX_VALUE, 300));
        base.add(pane);

        JPanel bPanel = new JPanel();
        bPanel.setLayout(new BoxLayout(bPanel, BoxLayout.X_AXIS));
        bPanel.setPreferredSize(new Dimension(Integer.MAX_VALUE, 40));
        bPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 40));

        bPanel.add(searchItem);
        bPanel.add(Box.createHorizontalGlue());
        bPanel.add(addBill);
        bPanel.add(Box.createRigidArea(new Dimension(10, 40)));
        bPanel.add(updateBill);
        bPanel.add(Box.createRigidArea(new Dimension(10, 40)));
        bPanel.add(cancelBill);
        base.add(bPanel);

        SpringUtilities.makeCompactGrid(base,
                3, 1, //rows, cols
                10, 10, //initialX, initialY
                10, 10);

        add(base, BorderLayout.CENTER);

        /*this.addComponentListener(new ComponentAdapter() {

            @Override
            public void componentShown(ComponentEvent e) {
                VstBillDataForm.this.setFocusable(true);
                VstBillDataForm.this.requestFocusInWindow();
            }
        });
        */

        //this.setFocusable(true);
        //searchItem.requestFocusInWindow();

    }

    private void addToTable(VstTableItemSelectable selectedObject) {

        // do the calculations
        VstBillTableModel tModel = (VstBillTableModel) dataTable.getModel();
        tModel.addItem(selectedObject);
    }
    
    public void addItem()
    {
    VstSelectionTableWindow stockSelectionTable = new VstSelectionTableWindow() {

                    protected VstTableItemModel installTableModel() {
                        java.util.List<VstTableItemSelectable> dataList = billManagerService.getAllStockItemsForSelectionTable();

                        // write a new constructor that accepts 'VstTableItem' - It seems like un-necesesorry
                        return new VstTableItemModel(dataList, VstGuiConstants.BILL_ITEM_COL_NAMES, VstGuiConstants.BILL_ITEM_COL_CLASSES);
                    }

                    protected void okButtonClicked() {
                        VstTableItemModel tModel = (VstTableItemModel) getModel();
                        if (getSelectedRow() == -1) {
                            return;
                        }

                        VstTableItemSelectable object = tModel.getObjecAtRow(getSelectedRow());

                        if (object != null) {
                            addToTable(object);
                        }
                    }
                };
                stockSelectionTable.setVisible(true);
    }

    private void initActions() {
        searchItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                addItem();
            }
        });

        cancelBill.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
            }
        });

        // KEY
        this.addKeyListener(new KeyListener() {

            /**
             * Handle the key typed event from the text field.
             */
            public void keyTyped(KeyEvent e) {
            }

            /**
             * Handle the key pressed event from the text field.
             */
            public void keyPressed(KeyEvent e) {
            }

            /**
             * Handle the key released event from the text field.
             */
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_F5)
                {
                    System.out.println("F5 pressed");
                    for(ActionListener a: searchItem.getActionListeners()) {
                        a.actionPerformed(null);
                    }
                }
            }
        });
    }
}